<?php
set_time_limit(0);
error_reporting(E_ALL);
require_once './Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '姓名')
            ->setCellValue('B1', '手机')
            ->setCellValue('C1', '年级')
            ->setCellValue('D1', '身份证')
            ->setCellValue('E1', '学校')
            ->setCellValue('F1', '分数')
            ->setCellValue('G1', '秒')
;
$dbhost = '47.110.131.55';
$dbuser = 'root';
$dbpwd  ='sxy7788123456';
$dbName ='race';

$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {}
 
$table='u3_c3';
//ALTER TABLE `u3_zz` ADD `miao` INT(10) NULL DEFAULT NULL AFTER `fen`;
$njZhongWen=array(
    'yc'=>'预初',
    'c1'=>'初一',
    'c2'=>'初二',
    'c3'=>'初三',
    'g1'=>'高一',
    'g2'=>'高二',
    'g3'=>'高三',
    'zz'=>'中职'
);

$sql="select * from $table  order by fen desc, miao asc  ";
$query =$mysqli->query($sql);
$index=2;
$fileName='';
while ($list=$query->fetch_array()){
    if($list['ischong'] != 1){
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$index, $list['name'])
        ->setCellValue('B'.$index, $list['phone'])
        ->setCellValue('C'.$index, $njZhongWen[$list['nianji']])
        ->setCellValue('D'.$index, $list['idcard'])
        ->setCellValue('E'.$index, $list['qu'].' '.$list['school'])
        ->setCellValue('F'.$index, $list['fen'])
        ->setCellValue('G'.$index, $list['miao'])
        ;
        $index++;
    }
	    if($index < 5){
	        $fileName=$njZhongWen[$list['nianji']];
	    }
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($fileName.'考试信息');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename='.$fileName.'考试信息.xlsx');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
